﻿using System.ComponentModel;
using System.Linq;
using FileSynchronizer.DataAccess;

namespace FileSynchronizer.Objects
{
    //---------------------------------------------------------------------------------------------------------------------------------------
    // Code based upon the State Pattern, as described by the GOF
    //
    // http://en.wikipedia.org/wiki/State_pattern and http://en.wikipedia.org/wiki/Talk:State_pattern#Disputed
    //
    // This is a Concrete State Object
    //
    //---------------------------------------------------------------------------------------------------------------------------------------

    class ProgramState_Initialized : ProgramState
    {
        #region Constructors
        //---------------------------------------------------------------------------------------------------------------------------------------
        public ProgramState_Initialized(ProgramContext TheProgramContext)
            : base(TheProgramContext)
        {
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion

        #region Public Methods
        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to go to the Idle State
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool GotoIdleState()
        {
            ChangeState(new ProgramState_Idle(CurrentProgramContext));
            CurrentProgramContext.CurrentState = State.Idle;

            return true;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Called when we want to Synchronize all FolderPairs
        //---------------------------------------------------------------------------------------------------------------------------------------
        public override bool SynchronizeAllFolderPairs(IQueryable<FolderPair> TheFolderPairs, RunWorkerCompletedEventHandler ExternalCompletionHandler)
        {
            bool OperationResult = false;
            ProgramState_AutoSync NextState;

            if (TheFolderPairs != null)
            {
                if (TheFolderPairs.ToList().Count() > 0)
                {
                    NextState = new ProgramState_AutoSync(CurrentProgramContext);
                    ChangeState(NextState);
                    CurrentProgramContext.CurrentState = State.AutoSync;

                    OperationResult = NextState.SynchronizeAllFolderPairs(TheFolderPairs, ExternalCompletionHandler);
                }
                else
                {
                    SyncFolderPairListCompleted(null, null);
                }
            }
            else
            {
                SyncFolderPairListCompleted(null, null);
            }

            return OperationResult;
        }

        //---------------------------------------------------------------------------------------------------------------------------------------
        // Callback Function which is called whenever the synchronization of ALL folderpairs is complete
        //---------------------------------------------------------------------------------------------------------------------------------------
        private void SyncFolderPairListCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            ChangeState(new ProgramState_Idle(CurrentProgramContext));
            CurrentProgramContext.CurrentState = State.Idle;
        }
        //---------------------------------------------------------------------------------------------------------------------------------------
        #endregion
    }
}
